let protobuf = require("protobuf")

const {ccclass, property} = cc._decorator;

@ccclass
export default class Helloworld extends cc.Component {

    @property(cc.Label)
    label: cc.Label = null;

    @property
    text: string = 'hello';

    public ws:WebSocket = null
    private builder:any = null;
    private package:string = "mini.game.net";

    onLoad () {
        this.ws = new WebSocket("ws://192.168.0.106:8001")
        this.ws.binaryType = "arraybuffer"
        this.ws.onopen = (event:any) => {
            console.log("------onopen-------")
        }
        this.ws.onmessage = (event:any) => {
            console.log("--------------------",event)
            let gaoshiqing = new Uint8Array(event.data)
            let buffer = this.decode("GoldChange",gaoshiqing)
            console.log("-----onmessage-------",buffer)
        }
        this.ws.onclose = (event:any) => {
            console.log("-----onclose-------")
        }
        this.ws.onerror = (event:any) => {
            console.log("-----onerror-------")
        }
    }

    start () {
        // init logic
        this.label.string = this.text;
        console.log("---------------------",protobuf)
        protobuf.load("mini_game_net.proto", (err, root) => {
            if (err) {
                console.log("PBHelper. protobuf.load");
                return;
            }
            this.builder = root
            console.log("------------------",root,err)
            let buffer = this.encode("GoldChange",{
                uid:123,
                delta:234,
            })
            console.log("------------start-----------")
            this.scheduleOnce(() => {
                this.ws.send(buffer)
            },7)
        });

        let buff = new ArrayBuffer(2)
        let typeArrat = new Uint32Array(buff)
        console.log("---------------",typeArrat.length)
    }

    //查找pbName对应的pb对象
    private findPbClass(pbName){
        if (this.builder && pbName) {
            let pbClass = this.builder.lookupType(this.package + "." + pbName);
            return pbClass;
        }
        return null;
    }

    //pb编码
    public encode(pbName, obj){
        let pbClass = this.findPbClass(pbName);
        if (pbClass) {
            //校验数据格式是否正确
            let errMsg = pbClass.verify(obj);
            if (errMsg) {
                return null;
            }

            //调用protobuf.js的编码接口
            let message = pbClass.create(obj);
            console.log("-------------------------",message)
            let buffer = pbClass.encode(message).finish();
            console.log("------------encode-------------",buffer.length)
            //  //二进制数据的长度+一个short的长度
            let sendBuf = new ArrayBuffer(buffer.length + 4);
            let dv = new DataView(sendBuf);
            dv.setInt16(0,123); //写入一个short值 起始位置以byte为计数的指定偏移量(byteOffset)处储存一个16-bit数
            dv.setInt16(2,buffer.length)
            // //将二进制数据写入
            let u8view = new Uint8Array(sendBuf,4); //跳过一个short的距离
            for (let i = 0, strLen = buffer.length; i < strLen; ++i){
                u8view[i] = buffer[i];
            }


            return sendBuf;
        }
        return null;
    }

    //pb解码
    public decode(pbName, buffer){
        let pbClass = this.findPbClass(pbName);
        if (pbClass) {
            //校验数据格式是否正确
            let errMsg = pbClass.verify(buffer);
            if (errMsg) {
                return null;
            }

            //调用protobuf.js的解码接口
            let message = pbClass.decode(buffer);
            let msgObj = pbClass.toObject(message);
            return msgObj;
        }
        return null;
    }

    public formatData(arrayBuffer){
        //protobuf 6.x版本需将ArrayBuffer转换为Uint8Array
        return new Uint8Array(arrayBuffer);
    }
}
